package com.cssw.fyzb.busi.api.user;

//import com.sw.fyzb.busi.cache.SmsTpls;

import com.cssw.fyzb.core.exception.BizException;
import com.cssw.fyzb.core.model.vo.user.RegisterUserInfo;
import com.cssw.fyzb.core.model.vo.user.UpdateLoginPwdVo;
import com.cssw.fyzb.core.model.vo.user.UpdateTradePwdVo;
import com.cssw.fyzb.busi.service.user.UserService;
import com.cssw.fyzb.busi.util.CodeUtil;
import com.cssw.fyzb.core.model.dodb.User;
import com.ydn.appserver.Action;
import com.ydn.appserver.MinaAppClient;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.simplecache.SimpleCache;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.cssw.fyzb.busi.cache.CacheKeys.*;
import static com.cssw.fyzb.busi.util.Bizs.*;

/**
 * 获取短信验证码
 *
 * @author Feng Chen
 */
@Function(description = "校验短信验证码", parameters = {
        @Parameter(name = "mobile", type = Type.String, description = "手机号码", required = true),
        @Parameter(name = "inviteCode", type = Type.String, description = "邀请码", required = false),
        @Parameter(name = "verifyCode", type = Type.String, description = "手机号码", required = true),
        @Parameter(name = "trade", type = Type.String, description = "业务类型 BT010：注册, BT011:登录, BT012:重置密码, BT013: 解绑设备, BT015 重置交易密码", required = true)
})
@Component
public class Verify implements Action {

    @Autowired
    private SimpleCache simpleCache;

    //    @Autowired
    private MinaAppClient smsClient;

    @Autowired
    private UserService userService;

    private static final Logger logger = LoggerFactory.getLogger(GetVerify.class);

    @Override
    public Response execute(Request request) throws Exception {
        String mobile = request.getString("mobile");
        String inviteCode = request.getString("inviteCode");
        String verifyCode = request.getString("verifyCode");
        String trade = request.getString("trade");
        String cached = simpleCache.get(forVerify(mobile));
        try {
            // 校验验证码
            if (!verifyCode.equals(cached)) {
                return Response.fail(ERROR_VERIFY.message, ERROR_VERIFY.code);
            }
            // 注册
            if ("BT010".equals(trade)) {
                User inviter = null;
                Long userId = null;
                if (StringUtils.isNotEmpty(inviteCode)) {
                    userId = CodeUtil.decode(inviteCode);
                    inviter = userService.getById(userId);
                    if (inviter == null) {
                        return Response.fail(ERROR_INVITCODE.message, ERROR_INVITCODE.code);
                    }
                } else {
                    return Response.fail(ERROR_INVITCODE.message, ERROR_INVITCODE.code);
                }

                if (userService.getByMobile(mobile) != null) {
                    return Response.fail(ERROR_REGISTERED.message, ERROR_REGISTERED.code);
                }
                RegisterUserInfo registerUserInfo = new RegisterUserInfo();
                registerUserInfo.setMobile(mobile);
                registerUserInfo.setInvitCode(inviteCode);
                registerUserInfo.setVerifyCode(verifyCode);
                simpleCache.put(forRegister(mobile), registerUserInfo, 5 * 60);
            }
            // 修改密码
            if ("BT014".equals(trade)) {
                UpdateLoginPwdVo updateLoginPwdVo = new UpdateLoginPwdVo();
                updateLoginPwdVo.setMobile(mobile);
                updateLoginPwdVo.setVerifyCode(verifyCode);
                simpleCache.put(forUpdateLoginPwd(mobile), updateLoginPwdVo, 5 * 60);
            }
            // 修改交易密码
            if ("BT015".equals(trade)) {
                UpdateTradePwdVo updateTradePwdVo = new UpdateTradePwdVo();
                updateTradePwdVo.setMobile(mobile);
                updateTradePwdVo.setVerifyCode(verifyCode);
                simpleCache.put(forUpdateTradePwd(mobile), updateTradePwdVo, 5 * 60);
            }

            return Response.success();
        } catch (Exception e) {
            if (e instanceof BizException) {
                BizException be = (BizException) e;
                return Response.fail(be.getErrMsg(), be.getErrCode());
            }
            logger.error("", e);
            return Response.fail("获取短信验证码失败");
        }
    }

}
